"""
类变量和实例变量
"""


class Company(object):
    total_num = 0

    def __init__(self, name):
        self.name = name


if __name__ == '__main__':
    company = Company("Google")
    # 访问实例变量
    print(company.name)
    print(company.total_num)
    # 输出结果：
    """
    Google
    0
    """

    # 访问类变量
    print(Company.total_num)
    # 输出结果：
    """
    0
    """

    # 修改类变量，其实例变量会跟着变化
    Company.total_num = 1
    print(company.total_num)
    print(Company.total_num)
    # 输出结果：
    """
    1
    1
    """

    # 修改实例变量，并不会改变类变量（其原理是动态添加一个相同的实例变量，涉及到python的自省机制）
    company.total_num = 2
    print(company.total_num)
    print(Company.total_num)
    # 输出结果：
    """
    2
    1
    """
